package com.example.cymjoe.huaweishopping;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import com.example.cymjoe.huaweishopping.adapter.MyAdapter;
import com.example.cymjoe.huaweishopping.fragment.FenLeiFragment;
import com.example.cymjoe.huaweishopping.fragment.HuaWeiFragment;
import com.example.cymjoe.huaweishopping.fragment.RongYaoFragment;
import com.example.cymjoe.huaweishopping.fragment.ShouYeFragment;
import com.example.cymjoe.huaweishopping.fragment.WoDeFragment;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {

    private ViewPager viewPager;
    private List<Fragment> data = new ArrayList<>();
    private RadioGroup radioGroup;
    private RadioButton rongyao;
    private RadioButton fenlei;
    private RadioButton huawei;
    private RadioButton shouye;
    private RadioButton wode;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        viewPager.setOffscreenPageLimit(4);
        viewPager.setAdapter(new MyAdapter(getSupportFragmentManager(), data, this));
        viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                switch (position) {
                    case 0:

                        shouye.setChecked(true);
                        break;
                    case 1:
                        huawei.setChecked(true);

                        break;
                    case 2:
                        rongyao.setChecked(true);

                        break;
                    case 3:

                        fenlei.setChecked(true);
                        break;
                    case 4:

                        wode.setChecked(true);
                        break;


                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
        radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                switch (checkedId) {
                    case R.id.shouye:
                        viewPager.setCurrentItem(0);

                        break;
                    case R.id.huawei:
                        viewPager.setCurrentItem(1);

                        break;
                    case R.id.rongyao:
                        viewPager.setCurrentItem(2);

                        break;
                    case R.id.fenlei:
                        viewPager.setCurrentItem(3);

                        break;
                    case R.id.wode:
                        viewPager.setCurrentItem(4);

                        break;


                }
            }
        });
    }

    private void initView() {
        radioGroup = ((RadioGroup) findViewById(R.id.radioGroup));
        viewPager = ((ViewPager) findViewById(R.id.viewPager));
        rongyao = ((RadioButton) findViewById(R.id.rongyao));
        fenlei = ((RadioButton) findViewById(R.id.fenlei));
        huawei = ((RadioButton) findViewById(R.id.huawei));
        shouye = ((RadioButton) findViewById(R.id.shouye));
        wode = ((RadioButton) findViewById(R.id.wode));
        ShouYeFragment shouYeFragment = new ShouYeFragment();
        HuaWeiFragment huaWeiFragment = new HuaWeiFragment();
        RongYaoFragment rongYaoFragment = new RongYaoFragment();
        FenLeiFragment fenLeiFragment = new FenLeiFragment();

        WoDeFragment woDeFragment = new WoDeFragment();
        data.add(shouYeFragment);
        data.add(huaWeiFragment);
        data.add(rongYaoFragment);
        data.add(fenLeiFragment);
        data.add(woDeFragment);
    }

    public void btnClick(View view) {
        finish();
    }
}
